package com.just.spider.action;

import com.just.spider.entity.BookInfo;
import com.just.spider.utils.ExcelUtil;
import com.just.spider.utils.SpiderHttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 爬取数据
 */
public class SpiderData {

    private static String rootUrl="https://book.douban.com/tag/编程";

    private List<BookInfo> bookInfoList=new ArrayList<BookInfo>();

    /**
     * 获取豆瓣关于编程类的书籍第一页的图书信息，第二版再考虑爬取所有页的信息
     */
    public void getOnePageData(){
        Document doc= Jsoup.parse(SpiderHttpUtils.readHtml(rootUrl));
        Elements booksElements=doc.getElementsByClass("subject-item");
        int order=0;
        for(Element bookElement:booksElements){
            Element bookInfoElement=bookElement.getElementsByClass("info").first();
            this.analysisDetailsPage(bookInfoElement.child(0).child(0).attr("abs:href"),order++);
        }
        System.out.println(booksElements.size());
        outExcel(bookInfoList);
    }

    /**
     * 从详情页爬取信息
     * @param detailUrl 详情页的链接
     */
    public void analysisDetailsPage(String detailUrl,int order){
        System.out.println("抓取的详情页面url:"+detailUrl);
        Document doc= Jsoup.parse(SpiderHttpUtils.readHtml(detailUrl));
        String bookName=doc.getElementsByTag("h1").text();
        String score=doc.getElementById("interest_sectl").getElementsByTag("strong").first().text();
        String ratingSum=doc.getElementsByClass("rating_people").first().child(0).text();
        Element infoElement=doc.getElementById("info");
        String info=infoElement.text();
        System.out.println(info);
        BookInfo bookInfo=analysisText(info);
        bookInfo.setOrder(order);
        bookInfo.setBookName(bookName);
        bookInfo.setRatingSum(ratingSum);
        bookInfo.setBookScore(score);
        bookInfoList.add(bookInfo);
    }

    /**
     * 处理图书信息，将对应的参数提取出来
     * @param info 图书信息字符串
     * @return
     */
    public static BookInfo analysisText(String info){
        String[] result=info.split(" ");
        boolean belongAuthor=false;
        String author="";
        String publisher="";
        String price="";
        String publishDate="";
        for(int i=0;i<result.length;i++){
            if("作者:".equals(result[i])){
                belongAuthor=true;
                continue;
            }
            if("出版社:".equals(result[i])){
                belongAuthor=false;
                publisher=result[i+1];
            }
            if("出版年:".equals(result[i])){
                publishDate=result[i+1];
            }
            if("定价:".equals(result[i])){
                price=result[i+1];
            }
            if(belongAuthor){
                author=author+result[i];
            }
        }
        BookInfo bookInfo=new BookInfo();
        bookInfo.setAuthor(author);
        bookInfo.setPrice(price);
        bookInfo.setPublisher(publisher);
        bookInfo.setPublishDate(publishDate);
        return bookInfo;
    }

    public static void outExcel(List<BookInfo> bookInfoList){
        String fileName="图书.xls";
        String filePath="D://";
        List<String> columnList=new ArrayList<String>();
        columnList.add("序号");
        columnList.add("书名");
        columnList.add("评分");
        columnList.add("评价人数");
        columnList.add("作者");
        columnList.add("出版社");
        columnList.add("出版日期");
        columnList.add("价格");
        List<String> fieldNameList=new ArrayList<String>();
        fieldNameList.add("order");
        fieldNameList.add("bookName");
        fieldNameList.add("bookScore");
        fieldNameList.add("ratingSum");
        fieldNameList.add("author");
        fieldNameList.add("publisher");
        fieldNameList.add("publishDate");
        fieldNameList.add("price");
        int maxSheetSize=100;
        ExcelUtil.excelOut(fileName,filePath,columnList,fieldNameList,bookInfoList,maxSheetSize);
    }
    public static void main(String [] args){
        new SpiderData().getOnePageData();
    }



}
